//
//  DistanceViewViewController.swift
//  Example
//
//  Created by xx on 2025/3/17.
//

import UIKit
import FastSwiftSDK
class RadarViewController: FastViewController {
    
    private lazy var logView : LogView = {
        let view =  LogView.init()
        self.view.addSubview(view)
        return  view
    } ()
    
    private lazy var selectView : SelectionView = {
        let view = SelectionView()
        self.view.addSubview(view)
        return  view
    } ()
    
    private lazy var radarView : RadarView = {
        let view = RadarView()
        view.callBack = { str in
            if str.isEmpty == true{
                
                self.distance()
                
            }else{
                self.turn(str)
            }
        }
        self.view.addSubview(view)
        return  view
    } ()
    
    var deviceList:Array<XXDevice>?
    var topic_distance = ""
    var topic_distance_a = ""
    var topic_trun = ""
    
    var topic_light = ""
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "雷达设备"
        self.deviceList = XXDeviceManager.shared.getDeviceList(nil)
        if let arr = self.deviceList{
            selectView.updateItems(devices: arr)
            
            topic_distance = "\(arr[0].type)/\(arr[0].mac)/D"
            topic_distance_a =  "\(arr[0].type)/\(arr[0].mac)/D_A"
            topic_trun = "\(arr[0].type)/\(arr[0].mac)/S"
            topic_light  = "\(arr[0].type)/\(arr[0].mac)/L"
            XXMQTT.shared.subseribes(topics: [topic_distance_a])
//            XXMQTT.shared.registerRoute(topic_distance_a) { msg in
//                if let str = msg{
//                    self.logView.addLog(str)
//                }
//                
//            }
        }
    }

    override func viewWillLayoutSubviews() {
        super.viewWillLayoutSubviews()
        selectView.snp.makeConstraints { make in
            make.top.equalTo(kNavH)
            make.left.equalTo(5)
            make.width.equalTo((kScreenW-15)/2)
            make.height.equalTo(50)
        }
        logView.snp.makeConstraints { make in
            make.top.equalTo(selectView.snp.bottom)
            make.left.equalTo(5)
            make.width.equalTo((kScreenW-15)/2)
            make.bottom.equalToSuperview()
        }
        radarView.snp.makeConstraints { make in
            make.top.equalTo(selectView.snp.bottom)
            make.right.equalTo(-5)
            make.width.equalTo(logView)
            make.bottom.equalToSuperview()
        }
    }
    
    func distance(){
        XXMQTT.shared.send(topic: topic_light, msg: "on")
    }
    
    func turn(_ msg:String){
        XXMQTT.shared.send(topic: topic_trun, msg: "90")
        Timer.scheduledTimer(withTimeInterval: 0.5, repeats: false) { [weak self] timer in
            if let topic = self?.topic_trun{
                XXMQTT.shared.send(topic: topic, msg: "180")
            }
        }
    }

}

class RadarView: FastView{
    
    var callBack:FastStrBlock?
    
    private lazy var txt_angle : FastTextField = {
        let txt =  FastTextField.init()
        txt.border(color: themeColor, width: 1)
        txt.cornerRadius = 5
        txt.keyboardType = .numberPad
        self.addSubview(txt)
        return  txt
    } ()
    
    private lazy var btn_distance : FastButton = {
        let btn = PublicView.button(title: "测距", style: .backgroundColor) { [weak self] in
            self?.callBack?("")
        }
        self.addSubview(btn)
        return  btn
    } ()
    
    private lazy var btn_turn : FastButton = {
        let btn = PublicView.button(title: "转动", style: .backgroundColor) { [weak self] in
            if let txt = self?.txt_angle.text{
                self?.callBack?(txt)
            }
        }
        self.addSubview(btn)
        return  btn
    } ()
    
    
    override func layoutSubviews() {
        super.layoutSubviews()
        txt_angle.snp.makeConstraints { make in
            make.top.equalToSuperview().offset(5)
            make.left.equalToSuperview().offset(5)
            make.right.equalToSuperview().offset(-5)
            make.height.equalTo(btnHeigth)
        }
        btn_turn.snp.makeConstraints { make in
            make.top.equalTo(txt_angle.snp.bottom).offset(10)
            make.left.equalTo(txt_angle)
            make.right.equalTo(txt_angle)
            make.height.equalTo(btnHeigth)
            
        }
        btn_distance.snp.makeConstraints { make in
            make.top.equalTo(btn_turn.snp.bottom).offset(10)
            make.left.equalTo(txt_angle)
            make.right.equalTo(txt_angle)
            make.height.equalTo(btnHeigth)
        }
    }
    
}
